package com.managerProvider.service.impl;

import com.managerProvider.mapper.PowerMapper;
import com.managerProvider.mapper.PrMapper;
import com.managerProvider.mapper.RoleMapper;
import com.managerProvider.mapper.UrMapper;
import com.managerProvider.service.RoleService;
import entity.ResponseMessage;
import entity.Role;
import entity.StatusCode;
import entity.Ur;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色服务impl
 * The type Role service.
 *
 * @author tapoliuliyue
 * @date 2022/04/23
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UrMapper urMapper;
    @Autowired
    private PowerMapper powerMapper;
    @Autowired
    private PrMapper prMapper;
    private Logger logger = Logger.getLogger(this.getClass());

    /**
     * Select all role response message.
     *
     * @param role the role
     * @return the response message
     */
    @Override
    public ResponseMessage selectAllRole(Role role) {
        ResponseMessage msg = new ResponseMessage();
        try {
            List<Role> roles = roleMapper.selectRole(role);
            if (roles == null || roles.size() == 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("RoleServiceImpl selectAllRole result is empty");
                logger.debug("RoleServiceImpl selectAllRole params:" + role + ",result:" + roles);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setFlag(true);
            msg.setMessage("ok");
            msg.setData(roles);
            logger.debug("RoleServiceImpl selectAllRole params:" + role + ",result:" + roles);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("RoleServiceImpl selectAllRole result is error");
            logger.debug("RoleServiceImpl selectAllRole params:" + role + ",result:" + e);
            return msg;
        }
    }

    /**
     * Insert role response message.
     *
     * @param role the role
     * @return the response message
     */
    @Override
    public ResponseMessage insertRole(Role role) {
        ResponseMessage msg = new ResponseMessage();
        try {
            int insert = roleMapper.insert(role);
            if (insert <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("RoleServiceImpl insertRole insert failed");
                msg.setFlag(false);
                logger.debug("RoleServiceImpl insertRole params:" + role + ",result:" + insert);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setMessage("ok");
            msg.setFlag(true);
            logger.debug("RoleServiceImpl insertRole params:" + role + ",result:" + insert);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("RoleServiceImpl insertRole insert error");
            msg.setFlag(false);
            logger.error("RoleServiceImpl insertRole params:" + role + ",result:" + e);
            return msg;
        }
    }

    /**
     * Update role response message.
     *
     * @param role the role
     * @return the response message
     */
    @Override
    public ResponseMessage updateRole(Role role) {
        ResponseMessage msg = new ResponseMessage();

        try {
            int update = roleMapper.updateById(role);
            if (update <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("RoleServiceImpl updateRole update failed");
                msg.setFlag(false);
                logger.debug("RoleServiceImpl updateRole params:" + role + ",result:" + update);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setMessage("ok");
            msg.setFlag(true);
            logger.debug("RoleServiceImpl insertRole params:" + role + ",result:" + update);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("RoleServiceImpl updateRole update error");
            msg.setFlag(false);
            logger.debug("RoleServiceImpl updateRole params:" + role + ",result:" + e);
            return msg;
        }
    }

    /**
     * Insert ur response message.
     *
     * @param ur the ur
     * @return the response message
     */
    @Override
    public ResponseMessage insertUr(Ur ur) {
        ResponseMessage msg = new ResponseMessage();
        try {
            int insert = urMapper.insert(ur);
            if (insert <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("RoleServiceImpl insertUr update failed");
                msg.setFlag(false);
                logger.debug("RoleServiceImpl insertUr params:" + ur + ",result:" + insert);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setMessage("ok");
            msg.setFlag(true);
            logger.debug("RoleServiceImpl insertUr params:" + ur + ",result:" + insert);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("RoleServiceImpl insertUr update error");
            msg.setFlag(false);
            logger.debug("RoleServiceImpl insertUr params:" + ur + ",result:" + e);
            return msg;
        }
    }

    @Override
    public ResponseMessage selectRoleAndPower(Role role) {
        ResponseMessage msg = new ResponseMessage();
        try {
            List<Role> roles = roleMapper.selectRoleAndPower(role);
            if (roles == null || roles.size() == 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("RoleServiceImpl selectRoleAndPower result is empty");
                logger.debug("RoleServiceImpl selectRoleAndPower params:" + role + ",result:" + roles);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setFlag(true);
            msg.setMessage("ok");
            msg.setData(roles);
            logger.debug("RoleServiceImpl selectRoleAndPower params:" + role + ",result:" + roles);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("RoleServiceImpl selectRoleAndPower result is error");
            logger.debug("RoleServiceImpl selectRoleAndPower params:" + role + ",result:" + e);
            return msg;
        }
    }

    @Override
    public ResponseMessage selectOneRolePower(Role oneRole) {
        ResponseMessage msg = new ResponseMessage();
        try {
            Role role = roleMapper.selectOneRolePower(oneRole);
            if (role == null) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("RoleServiceImpl selectOneRolePower result is empty");
                logger.debug("RoleServiceImpl selectOneRolePower params:" + oneRole + ",result:" + role);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setFlag(true);
            msg.setMessage("ok");
            msg.setData(role);
            logger.debug("RoleServiceImpl selectOneRolePower params:" + oneRole + ",result:" + role);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("RoleServiceImpl selectOneRolePower result is error");
            logger.debug("RoleServiceImpl selectOneRolePower params:" + oneRole + ",result:" + e);
            return msg;
        }
    }
}
